
class FlazXPlugin:
	def __init__(self):
		# put all the links this plugins supports here - it will be used to filter supported URLs
		self.supported_sites = ['www.flazx.com']
		self.supported_urls = ['www.flazx.com/ebook']

	def get(self, url):
		"""Get a book from www.flazx.com"""
		data = urllib2.urlopen(url).read()
		parser = URLLister()
		parser.feed(data)
		for script_url in parser.script_urls:
			if script_url.find('www.flazx.info') != -1:
				url = script_url
				break
		parser.close()
		get_next_line = False
		book_name = 'Unknown'
		year = 'None'
		for line in data.splitlines():
			if line.find('<title>') != -1:
				book_name = line.split('<title>FlazX - ')[1].split('<')[0].strip()
			if get_next_line:
				year = line.split('<')[0].split()[2]
				break
			if line.find('Release Date') != -1:
				get_next_line = True
		del data
		book_name = get_valid_name(book_name)
		mydir = '/' + book_name + ' (' + year + ')'
		try: os.mkdir(config.dl_dir + mydir)
		except: pass

		for line in urllib2.urlopen(url).read().splitlines():
			if line.find('href') != -1:
				url = line.split("href='")[1].split("'")[0]
				break

		parser.feed(urllib2.urlopen(url).read())
		for src_url in parser.urls:
			if src_url.find('www.flazx.info') != -1:
				url = src_url
		parser.close()
		local_file = get_filename_from_url(url)
		file_to_save = os.path.join(config.dl_dir + mydir, local_file)
		download_file_with_retries(file_to_save, url)
		extract_all(file_to_save)
		config.report.report_finish(local_file)

	def url_matches(self, url):
		"""Check whether we support this URL"""
		for l in self.supported_urls:
			if url.find(l) != -1:
				return True
		return False

	def version(self):
		"""Return plugin version - when interface changes, the version changes"""
		return 1

modules = [FlazXPlugin()]
